package de.gwtprime.server.sql;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import de.gwtprime.server.model.ArticleSection;


/**
 * Use a singelton to get the SessionFactory
 * @author jan
 *
 */
public enum HibernateEntityManagerSingeltonNew {

	INSTANCE;
	
	private static SessionFactory sessionFactory = configureSessionFactory();
	private static ServiceRegistry serviceRegistry;
	
	private static SessionFactory configureSessionFactory() throws HibernateException {
	   try{
		Configuration configuration = new Configuration();
	    configuration.addAnnotatedClass(ArticleSection.class);
	    configuration.configure("hibernateStaging.cfg.xml");
	    ServiceRegistryBuilder  ssrb = new ServiceRegistryBuilder();
	    
	    ssrb.applySettings(configuration.getProperties());
	    serviceRegistry = ssrb.buildServiceRegistry();
	    MetadataSources mds = new MetadataSources(serviceRegistry);
	    
	    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
	   }catch(Exception e){
		   System.out.println(e);
		   e.printStackTrace();
		   
	   }
	    return sessionFactory;
	}
	
	public static SessionFactory getSessionFactory() {
		 return sessionFactory;
	 }
}
